<template>
  <div style="margin: 10px">
    <el-button type="primary" @click="handleAdd">新增员工</el-button>
  </div>
  <el-table :data="filterTableData" style="width: 100%">
    <el-table-column label="姓名" prop="name" />
    <el-table-column label="年龄" prop="age" />
    <el-table-column label="性别" prop="sex" />
    <el-table-column label="联系电话" prop="phone" />
    <el-table-column label="籍贯" prop="address" />
    <el-table-column align="right">
      <template #header>
        <el-input v-model="search" size="small" placeholder="请输入姓名" />
      </template>
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
          编辑
        </el-button>
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="500"
    :before-close="handleClose"
  >
    <el-form
      ref="ruleFormRef"
      :rules="rules"
      :model="form"
      label-width="auto"
      style="max-width: 600px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="form.age" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-input v-model="form.sex" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="籍贯" prop="address">
        <el-input v-model="form.address" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm(ruleFormRef)">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { computed, reactive, ref } from "vue";
const search = ref("");
const staffList = ref([
  {
    id: 1,
    name: "张三",
    age: 25,
    sex: "男",
    phone: "12345678910",
    address: "北京市海淀区",
  },
  {
    id: 2,
    name: "李四",
    age: 30,
    sex: "女",
    phone: "12345678911",
    address: "上海市浦东新区",
  },
  {
    id: 3,
    name: "王五",
    age: 28,
    sex: "男",
    phone: "12345678912",
    address: "广州市天河区",
  },
  {
    id: 4,
    name: "赵六",
    age: 35,
    sex: "女",
    phone: "12345678913",
    address: "深圳市南山区",
  },
  {
    id: 5,
    name: "钱七",
    age: 27,
    sex: "男",
    phone: "12345678914",
    address: "成都市锦江区",
  },
]);
const dialogVisible = ref(false);
const title = ref("编辑信息");
const form = reactive({
  id: "",
  name: "",
  age: "",
  sex: "",
  phone: "",
  address: "",
});
const ruleFormRef = ref(null);
const rules = reactive({
  name: [
    {
      required: true,
      message: "请输入姓名",
      trigger: "blur",
    },
  ],
  age: [
    {
      required: true,
      message: "请输入年龄",
      trigger: "blur",
    },
  ],
  sex: [
    {
      required: true,
      message: "请输入性别",
      trigger: "blur",
    },
  ],
  phone: [
    {
      required: true,
      message: "请输入联系电话",
      trigger: "blur",
    },
  ],
  address: [
    {
      required: true,
      message: "请输入籍贯",
      trigger: "blur",
    },
  ],
});

const filterTableData = computed(() =>
  staffList.value.filter(
    (data) =>
      !search.value ||
      data.name.toLowerCase().includes(search.value.toLowerCase())
  )
);
const handleEdit = (index, row) => {
  form.id = row.id;
  form.name = row.name;
  form.age = row.age;
  form.sex = row.sex;
  form.phone = row.phone;
  form.address = row.address;
  title.value = "编辑信息";
  dialogVisible.value = true;
};

const handleClose = (done) => {
  done();
};

const close = () => {
  form.id = "";
  form.name = "";
  form.age = "";
  form.sex = "";
  form.phone = "";
  form.address = "";
  dialogVisible.value = false;
};
const handleAdd = () => {
  form.id = "";
  form.name = "";
  form.age = "";
  form.sex = "";
  form.phone = "";
  form.address = "";
  title.value = "新增员工";
  dialogVisible.value = true;
};

const handleDelete = (index, row) => {
  staffList.value = staffList.value.filter((v) => v.id !== row.id);
  localStorage.setItem("staffList", JSON.stringify(staffList.value));
};

const confirm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (title.value === "新增员工") {
        form.id = staffList.value.length + 1;
        staffList.value.push({
          id: form.id,
          name: form.name,
          age: form.age,
          sex: form.sex,
          phone: form.phone,
          address: form.address,
        });
      } else {
        staffList.value = staffList.value.map((item) => {
          if (item.id === form.id) {
            return {
              id: form.id,
              name: form.name,
              age: form.age,
              sex: form.sex,
              phone: form.phone,
              address: form.address,
            };
          } else {
            return item;
          }
        });
      }
      dialogVisible.value = false;
      form.id = "";
      form.name = "";
      form.age = "";
      form.sex = "";
      form.phone = "";
      form.address = "";
      localStorage.setItem("staffList", JSON.stringify(staffList.value));
    } else {
      console.log("error submit!", fields);
    }
  });
};

const init = () => {
  let local = JSON.parse(localStorage.getItem("staffList"));
  if (local) {
    staffList.value = local;
  }
};
init();
</script>

<style lang="less" scoped>
</style>